function emr = emr_read(emrfiles,emr)
% emr = emr_read(emrfiles)
%
% Author: Mads Dyrholm

if ~iscell(emrfiles), emrfiles = {emrfiles}; end

for e = 1:length(emrfiles)
  
  emrfile = emrfiles{e};
  [fields,vals] = textread(emrfile,'%s%s');
  
  for k = 1:length(fields)
    fieldk =  fields{k};
    valk   =  vals{k};
    switch fieldk
     case 'input'
      PATHSTR = fileparts(emrfile);
      if length(PATHSTR)>0, PATHSTR = [PATHSTR '/']; end
      emr = emr_read([PATHSTR valk]);
     case 'path'
      emr.epath = valk;
      emr.mrpath = valk;
     case 'mrpath'
      emr.mrpath = valk;
     case 'epath'
      emr.epath = valk;
     case 'mrfile'
      emr.mrfile = valk;
     case 'TR'
      emr.TR = eval(valk);
     case 'efile'
      emr.efile = valk;
     case 'eventfile'
      emr.eventfile = valk;
     case 'Fs'
      emr.Fs = eval(valk);
     case 'channel'
      emr.channel = str2num(valk);
     case 'quantizer'
      emr.quantizer = eval(valk);
     case 'contrast'
      emr.contrast = eval(valk);
     case 'versus'
      emr.versus = eval(valk);
     case 'roicorner1' % deprecate
      emr.roicorner1 = eval(valk);
     case 'roicorner2' % deprecate
      emr.roicorner2 = eval(valk);
     case 'roicorner'
      try
        emr.roicorner{length(emr.roicorner)+1} = eval(valk);
      catch
        emr.roicorner{1} = eval(valk);
      end
     case 'roisize'
      emr.roisize = eval(valk);
     case 'steadyfor'
      emr.steadyfor = str2num(valk);
     case 'multiblock'
      emr.multiblock = str2num(valk);
     case 'trgap'
      emr.trgap = str2num(valk);  
     otherwise
      if fieldk(1)~='#'
	warning(['in emr file, unknown field: ' fieldk]);
      end
    end
  end
end
